<template>
    <div class="goods-container">
        <!-- 左侧图片区域 -->
        <div class="left">
            <div class="custom-control custom-checkbox">
                <input type="checkbox" class="custom-control-input" :id="id" @change="onChecked" :checked="checked">
                <label class="custom-control-label" :for="id">
                    <img :src="thumb" alt="商品图片" class="thumb">
                </label>
            </div>

        </div>
        <!-- 右侧信息区域 -->
        <div class="right">
            <!-- 名称 -->
            <div class="top">{{ title }}</div>
            <div class="bottom">
                <!-- 商品价格 -->
                <div class="price">￥{{ price.toFixed(2) }}</div>
                <!-- 商品数量 -->
                <div class="count">
                    <EsCount :num="count" :min="1" @numchange="getChangeNum"></EsCount>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import EsCount from '../EsCount/index.vue';
export default {
    name: "EsGoods",
    emits: ["stateChange", "countChange"],
    props: {
        // 唯一key值
        id: {
            type: [Number, String],
            required: true
        },
        // 图片
        thumb: {
            type: String,
            required: true
        },
        // 商品名称
        title: {
            type: String,
            required: true
        },
        // 单价
        price: {
            type: Number,
            required: true
        },
        // 数量
        count: {
            type: Number,
            required: true
        },
        // 勾选状态
        checked: {
            type: Boolean,
            default: false
        }
    },
    computed: {},
    methods: {
        onChecked(e) {
            this.$emit("stateChange", {
                id: this.id,
                value: e.target.checked
            });
        },
        getChangeNum(num) {
            this.$emit("countChange", {
                id: this.id,
                value: num
            })
        }
    },
    components: { EsCount }
}
</script>

<style lang="less" scoped>
.goods-container {
    +.goods-container {
        border-top: 3px solid #efefef;
    }

    display: flex;
    padding: 10px;

    // 左侧图片
    .left {
        margin-right: 10px;

        .thumb {
            display: block;
            width: 100px;
            height: 100px;
            background-color: #efefef;
        }
    }

    // 右侧信息
    .right {
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        flex: 1;

        .top {
            font-weight: bold;
        }

        .bottom {
            display: flex;
            justify-content: space-between;
            align-items: center;

            .price {
                color: red;
                font-weight: bold;
            }
        }

    }
}

.custom-control-label::before,
.custom-control-label::after {
    top: 1.25rem !important;
}

.count {
    margin: 0 10px;
    font-size: 20px;
    font-weight: bold;
}
</style>